/*
 * Copyright (c) 2017,安徽皖通科技股份有限公司 All rights reserved.
 */

package com.wttech.dzzf.process.service.error;

import com.wttech.dzzf.process.config.FileConfig;
import com.wttech.dzzf.process.dao.GeneratorErrorDataDao;
import com.wttech.dzzf.process.model.error.TelMissing;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileWriter;
import java.util.List;

import static com.wttech.dzzf.process.constant.UserConstants.DEFAULT_PROCESS_NUM;

/**
 * 生成错误数据服务实现
 *
 * @author MaShibo
 * @date 2017/12/1
 */
@Service
public class GeneratorErrorDataService {

	private static final Logger LOGGER = LoggerFactory.getLogger(GeneratorErrorDataService.class);

	private static final String EMPTY = "		";


	@Resource
	private FileConfig config;

	@Resource
	private ThreadPoolTaskExecutor executor;


	@Resource
	private GeneratorErrorDataDao generatorErrorDataDao;


	private void generatorTelMissingInfos() {
		int total = generatorErrorDataDao.queryTelMissingNum();
		LOGGER.info("共【{}】条缺失手机号的数据", total);
		int count = (int) Math.ceil((double) total / DEFAULT_PROCESS_NUM);
		LOGGER.info("共需要处理【{}】次", count);
		for (int i = 0; i < count; i++) {
			int num = i + 1;
			LOGGER.info("第{}次，开始执行...", num);
			int startRow = (num - 1) * DEFAULT_PROCESS_NUM + 1;
			int endRow = num * DEFAULT_PROCESS_NUM;
			List<TelMissing> telMissings = generatorErrorDataDao.queryTelMissingInfos(startRow, endRow);
			String filePath = config.getTelMissingFilePath();
			String fileName = config.getTelMissingFileName() + "_" + num;
			String file = filePath + File.separator + fileName + ".txt";
			telMissings.forEach(telMissing -> writeToFile(telMissing, file));
			LOGGER.info("第{}次，执行结束...", num);
		}
	}

	private void generatorAllCardNos() {
		int total = generatorErrorDataDao.queryZHCardNum();
		LOGGER.info("共【{}】条卡片信息", total);
		int totalNum = (int) Math.ceil((double) total / DEFAULT_PROCESS_NUM);
		LOGGER.info("共需要处理【{}】次", totalNum);
		for (int i = 0; i < totalNum; i++) {
			int num = i + 1;
			int startRow = (num - 1) * DEFAULT_PROCESS_NUM + 1;
			int endRow = num * DEFAULT_PROCESS_NUM;
			executor.execute((() -> {
				LOGGER.info("第{}次，开始执行...", num);
				List<String> cardNos = generatorErrorDataDao.queryZHAllCardNos(startRow, endRow);
				StringBuilder cardLines = new StringBuilder();
				cardNos.forEach(cardNo -> cardLines.append(cardNo).append("\r\n"));
				String filePath = config.getCardFilePath();
				String fileName = config.getCardFileName();
				String file = filePath + File.separator + fileName + "_" + num + ".txt";
				try (FileWriter writer = new FileWriter(new File(file))) {
					writer.write(cardLines.toString());
				} catch (Exception e) {
					LOGGER.error(e.getMessage(), e);
				}
				LOGGER.info("第{}次，结束执行...", num);
			}));
		}
	}


	private void writeToFile(TelMissing telMissing, String file) {
		StringBuilder info = new StringBuilder();
		info.append(telMissing.getEcardNo()).append(EMPTY).append(telMissing.getFundAccountName())
				.append(EMPTY).append(telMissing.getCertifiType()).append(EMPTY).append(telMissing.getCertifiNo())
				.append(EMPTY).append(telMissing.getOpton()).append("\r\n");

		try (FileWriter fw = new FileWriter(file, true)) {
			fw.write(info.toString());
		} catch (Exception e) {
			LOGGER.error(e.getMessage(), e);
		}
	}

	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		GeneratorErrorDataService service = context.getBean(GeneratorErrorDataService.class);
		service.generatorAllCardNos();
	}

}
